<!DOCTYPE html>
<html lang="${LANG}" class="dark">

<head>
    <meta charset="UTF-8">
    <title>QBT_TR(Rename)QBT_TR[CONTEXT=TransferListWidget]</title>
    <link rel="stylesheet" href="css/style.css?v=${CACHEID}" type="text/css">
    <script defer src="scripts/localpreferences.js?v=${CACHEID}"></script>
    <script defer src="scripts/color-scheme.js?v=${CACHEID}"></script>
    <script>
        "use strict";

        window.addEventListener("DOMContentLoaded", (event) => {
            window.addEventListener("keydown", (event) => {
                switch (event.key) {
                    case "Enter":
                        event.preventDefault();
                        document.getElementById("renameButton").click();
                        break;
                    case "Escape":
                        event.preventDefault();
                        window.parent.qBittorrent.Client.closeFrameWindow(window);
                        break;
                }
            });

            const searchParams = new URLSearchParams(window.location.search);
            const name = searchParams.get("name");
            // set text field to current value
            if (name !== null)
                document.getElementById("rename").value = name;

            document.getElementById("rename").focus();
            document.getElementById("renameButton").addEventListener("click", (e) => {
                e.preventDefault();
                e.stopPropagation();

                // check field
                const name = document.getElementById("rename").value.trim();
                if ((name === null) || (name === ""))
                    return;

                const hash = searchParams.get("hash");
                if (hash !== null) {
                    fetch("api/v2/torrents/rename", {
                            method: "POST",
                            body: new URLSearchParams({
                                hash: hash,
                                name: name
                            })
                        })
                        .then((response) => {
                            if (!response.ok)
                                return;

                            window.parent.qBittorrent.Client.closeFrameWindow(window);
                        });
                }
            });
        });
    </script>
</head>

<body>
    <div style="padding: 10px 10px 0px 10px;">
        <label for="rename" style="font-weight: bold;">QBT_TR(New name:)QBT_TR[CONTEXT=TransferListWidget]</label>
        <input type="text" id="rename" style="width: 99%;">
        <div style="text-align: center; padding-top: 10px;">
            <input type="button" value="QBT_TR(Save)QBT_TR[CONTEXT=HttpServer]" id="renameButton">
        </div>
    </div>
</body>

</html>
